package cn.nexttec.noteplayer.ui.widget.image_editor

import android.content.Context
import android.graphics.*
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import cn.nexttec.noteplayer.R
import cn.nexttec.noteplayer.ui.widget.PopupDialog

class DrawingView(context: Context) : FrameLayout(context) {

    // 绘制路径和画笔的数据结构
    private data class Stroke(val path: Path, val paint: Paint)

    private val strokes = mutableListOf<Stroke>() // 所有线条
    private var currentPath: Path? = null
    private var currentColor = Color.BLACK
    private var isDrawing = false

    private val canvasView = object : View(context) {
        override fun onDraw(canvas: Canvas) {
            strokes.forEach { stroke ->
                canvas.drawPath(stroke.path, stroke.paint)
            }
        }
    }

    var onDrawingSavedCallback: (() -> Unit)? = null

    private val toolbar: View = LayoutInflater.from(context).inflate(R.layout.layout_drawing_toolbar, this, false)

    init {
        addView(canvasView)
        addView(toolbar)
        setupToolbar()
        visibility = GONE
        clearDrawing()
    }

    private fun setupToolbar() {
        toolbar.findViewById<View>(R.id.iv_color).setOnClickListener {
            PopupDialog.showColorPicker {
                currentColor = it
            }
        }

        toolbar.findViewById<View>(R.id.btn_reset_drawing).setOnClickListener {
            strokes.clear()
            canvasView.invalidate()
        }

        toolbar.findViewById<View>(R.id.btn_save_drawing).setOnClickListener {
            enableDrawing(false)
            onDrawingSavedCallback?.invoke() // 新增：通知 ImageEditorView 应用绘图
            performSaveCallback?.invoke()
        }

        toolbar.findViewById<View>(R.id.btn_exit_drawing).setOnClickListener {
            enableDrawing(false)
            performExitCallback?.invoke()
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (!isDrawing) return false

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                currentPath = Path().apply {
                    moveTo(event.x, event.y)
                }
                val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
                    color = currentColor
                    strokeWidth = 5f
                    style = Paint.Style.STROKE
                }
                strokes.add(Stroke(currentPath!!, paint))
            }

            MotionEvent.ACTION_MOVE -> {
                currentPath?.lineTo(event.x, event.y)
            }
        }

        canvasView.invalidate()
        return true
    }

    fun enableDrawing(enable: Boolean) {
        isDrawing = enable
        visibility = if (enable) VISIBLE else GONE
    }

    fun isDrawingMode(): Boolean = isDrawing

    // 让外部设置保存行为
    var performSaveCallback: (() -> Unit)? = null
    var performExitCallback: (() -> Unit)? = null

    // 清除画布
    fun clearDrawing() {
        strokes.clear()
        canvasView.invalidate()
    }

    // 将当前内容绘制到传入 canvas 上
    fun drawToCanvas(canvas: Canvas) {
        strokes.forEach { stroke ->
            canvas.drawPath(stroke.path, stroke.paint)
        }
    }
}
